/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package server.persistencia;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import server.dominio.Beneficiario;
import server.dominio.Cita;
import server.dominio.Horario;
import server.dominio.Medico;

/**
 *
 * @author tpaxle
 */
public class DAOCitasMySQL extends DAOCita{
	private Agente Broker;
	private Connection Con;

	public DAOCitasMySQL() throws  SQLException{
		Broker= Agente.getAgente();
		Con= Broker.getConnection();
	}

	@Override
	public int delete(Object cita) throws SQLException  {
		// TODO Auto-generated method stub
		PreparedStatement stmt;
		int result= 0;

		stmt = (PreparedStatement) Con.prepareStatement("DELETE FROM Cita WHERE idCita_pk= ?");
//		stmt.setInt(1, ((Cita)cita).getIdCita_pk());
		result= stmt.executeUpdate();

		return result;
	}

	@Override
	public int insert(Object obj) throws SQLException {
		Cita cita_aux= (Cita)obj;
		int resul=0;
		PreparedStatement stmt = (PreparedStatement) Con.prepareStatement("INSERT INTO Cita(idBeneficiario_fk, idMedico_fk, idfechaHora) VALUES (?,?,(SELECT id_Horario FROM Horario WHERE ? = Horario.id_Medico_fk and Fecha_trabajo= ?))");

//		stmt.setInt(1, cita_aux.getBeneficiario().getIdBeneficiario_pk());
//                stmt.setInt(2, cita_aux.get);
 //               stmt.setInt(3, cita_aux.getMedico().getIdMedico_pk());
  //              stmt.setString(4, cita_aux.getFechaHora());


		resul= stmt.executeUpdate();

             stmt.close();



		return resul;
	}

	public ArrayList<Object> select(String condicion) throws SQLException{
		ArrayList<Object> citas = new ArrayList<Object>();
		Statement stmt;
                DAOBeneficiarioMySQL daob= new DAOBeneficiarioMySQL();
                DAOMedicoMySQL daom = new DAOMedicoMySQL();
                DAOHoraMySQL daoh= new DAOHoraMySQL();
		stmt = (Statement) Con.createStatement();
		ResultSet res  = stmt.executeQuery(condicion);
		 while(res.next()){
                     Beneficiario bene= (Beneficiario) daob.find("SELECT * FROM Beneficiario WHERE idBeneficiario_pk= '"+ res.getString("idBeneficiario_fk") +"'");
                     Medico medico= bene.getMedico();
                     Horario hora=(Horario) daoh.find("SELECT * FROM Horario WHERE id_Horario= "+res.getInt("idfechaHora"));
                     int idCita_pk= res.getInt("idCita_pk");
    //                 Cita aux= new Cita(idCita_pk, bene, medico, hora.getFecha_trabajo());
//                     citas.add(aux);
                }
		 res.close();
		 stmt.close();


		return citas;
	}

	@Override
	public int update(Object obj) {
		// TODO Auto-generated method stub
			return 1;
	}

	@Override
	public Object find(String condicion) throws SQLException {
		// TODO Auto-generated method stub

		return null;
	}

	public void cerrarDAO() throws SQLException{
		Con.close();
	}
}
